package com.example.yiwei.crm.view.activity.contactActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.view.ContextThemeWrapper;
import android.support.v7.widget.PopupMenu;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.dao.customerDao;
import com.example.yiwei.crm.data.GetContact;
import com.example.yiwei.crm.data.GetContactData;
import com.example.yiwei.crm.dao_impl.customerDaoImpl;
import com.example.yiwei.crm.entity.ContactMessageEntity;
import com.example.yiwei.crm.util.NetWorkUtils;
import com.example.yiwei.crm.view.activity.customerActivity.CustomerDetailedMessageActivity;
import com.example.yiwei.crm.view.frament.contactDetailedMenu.contactBaseFragment;
import com.example.yiwei.crm.view.frament.contactDetailedMenu.contactOtherFragment;
import com.example.yiwei.crm.view.frament.contactDetailedMenu.contactQRCFragment;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class ContactDetaledMessageActivity extends AppCompatActivity {
    FragmentManager manager;
    //声明碎片
    Fragment fragment_base,fragment_other,fragment_QRC;
    Button btn_base,btn_other,btn_QRC;
    private PopupMenu popupMenu;
    private Menu menu;
    private ImageView img_update,img_call,img_messgae;
    ContactMessageEntity contact;
    private static final String Tag  = "update";
    TextView txt_title_name;
    boolean ButtonTag2 =false;
    boolean ButtonTag1 =false;
    boolean ButtonTag4 =false;
    int contact_id;
    AlertDialog.Builder builder;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_detaled_message);
        manager = getSupportFragmentManager();
        txt_title_name= (TextView) findViewById(R.id.contact_detailed_txt_name);
        btn_base = (Button) findViewById(R.id.contact_detailed_base);
        btn_other = (Button) findViewById(R.id.contact_detailed_other);
        btn_QRC = (Button) findViewById(R.id.contact_detailed_QRC);
        fragment_base = new contactBaseFragment();
        fragment_other = new contactOtherFragment();
        fragment_QRC = new contactQRCFragment();
        img_update= (ImageView) findViewById(R.id.contact_detailed_img_update);
        img_call= (ImageView) findViewById(R.id.contact_detailed_img_call);
        img_messgae= (ImageView) findViewById(R.id.contact_detailed_img_message);
        img_update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(ContactDetaledMessageActivity.this,ContactAddMessageActivity.class);
                intent.putExtra("tag",Tag);
                startActivity(intent);
            }
        });
        OnClick(btn_base);
        popupMenu = new PopupMenu(this, findViewById(R.id.contact_detailed_img_btn_plus));
        menu = popupMenu.getMenu();
        MenuInflater inflater = getMenuInflater();
        //设置menu界面为res/menu/menu.xml
        inflater.inflate(R.menu.menu_contact,menu);
        Intent intent=getIntent();
        contact_id=intent.getExtras().getInt("id");
//        下拉框监听方法
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.delete_contact:
                        delete_dialog();
                        break;
                    case R.id.change:

                }
                return false;
            }
        });
        contact= GetContact.get();
        img_call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (contact.getContact_mobile_phone().equals("")){
                    Toast.makeText(ContactDetaledMessageActivity.this, "无联系人电话，请填写完整信息！", Toast.LENGTH_SHORT).show();
                }else {
                    builder = new AlertDialog.Builder(new ContextThemeWrapper(ContactDetaledMessageActivity.this, R.style.custom_dialog));
                    builder.setTitle("确定要拨打电话吗？");
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            Intent intent = new Intent(Intent.ACTION_DIAL);
                            Uri data = Uri.parse("tel:" + contact.getContact_mobile_phone());
                            intent.setData(data);
                            startActivity(intent);
                        }
                    });
                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {

                        }
                    });
                    builder.show();
                }
            }
        });
        img_messgae.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (contact.getContact_mobile_phone().equals("")){
                    Toast.makeText(ContactDetaledMessageActivity.this, "无联系人电话，请填写完整信息！", Toast.LENGTH_SHORT).show();
                }else {
                    builder = new AlertDialog.Builder(new ContextThemeWrapper(ContactDetaledMessageActivity.this, R.style.custom_dialog));
                    builder.setTitle("您确定要发送短信吗？");
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + contact.getContact_mobile_phone()));
                            startActivity(intent);

                        }
                    });
                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {

                        }
                    });
                    builder.show();
                }
            }

        });
        txt_title_name.setText(contact.getContact_name());
    }
    public void delete_dialog(){
        AlertDialog.Builder builder;
        builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.custom_dialog));
        builder.setTitle("您确定要删除吗？");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String result= NetWorkUtils.getCount(baseClass.delete_contact_url+contact_id,null);
                        try {
                            JSONObject json=new JSONObject(result);
                            String boolen=json.getString("boolean");
                            if ("success".equals(boolen)){
                                customerDao customerdao=new customerDaoImpl();
                                List<ContactMessageEntity> contactMessageEntities = customerdao.queryContactAllMessage(baseClass.queryall_contact_message_url, null);
                                GetContactData.creatData();
                                GetContactData.contacts = contactMessageEntities;
                                if (GetContactData.contacts!=null){
                                    finish();
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {

            }

        });
        builder.show();
    }
    public void OnClick(View view) {
        switch (view.getId()){
            case R.id.contact_detailed_base:
                ButtonTag2=false;
                ButtonTag1 = true;
                ButtonTag4 = false;
                btn_base.setBackgroundColor(Color.parseColor("#FFFFFF"));
                btn_other.setBackgroundColor(Color.parseColor("#E1E8ED"));
                btn_QRC.setBackgroundColor(Color.parseColor("#E1E8ED"));
                manager.beginTransaction().replace(R.id.contactmee_frame_conner,fragment_base).commit();
                break;
            case R.id.contact_detailed_other:
                ButtonTag1 = false;
                ButtonTag2=true;
                ButtonTag4 = false;
                btn_base.setBackgroundColor(Color.parseColor("#E1E8ED"));
                btn_other.setBackgroundColor(Color.parseColor("#FFFFFF"));
                btn_QRC.setBackgroundColor(Color.parseColor("#E1E8ED"));
                manager.beginTransaction().replace(R.id.contactmee_frame_conner,fragment_other).commit();
                break;
            case R.id.contact_detailed_QRC:
                ButtonTag1 = false;
                ButtonTag2=false;
                ButtonTag4 = true;
                btn_base.setBackgroundColor(Color.parseColor("#E1E8ED"));
                btn_other.setBackgroundColor(Color.parseColor("#E1E8ED"));
                btn_QRC.setBackgroundColor(Color.parseColor("#FFFFFF"));
                manager.beginTransaction().replace(R.id.contactmee_frame_conner,fragment_QRC).commit();
                break;
            case R.id.contact_detailed_img_btn_plus:
                popupMenu.show();
                break;
            case R.id.contact_detailed_img_btn_back:
                finish();
        }
    }
    @Override
    protected void onRestart() {
        super.onRestart();
        contact=null;
        contact=GetContact.get();
        txt_title_name.setText(contact.getContact_name());
        if (ButtonTag1){
             fragment_base=new contactBaseFragment();
            manager.beginTransaction().replace(R.id.contactmee_frame_conner,fragment_base).commitAllowingStateLoss();
        }else if(ButtonTag2){
            fragment_other=new contactOtherFragment();
            manager.beginTransaction().replace(R.id.contactmee_frame_conner,fragment_other).commitAllowingStateLoss();
        }else if(ButtonTag4){
            fragment_QRC=new contactQRCFragment();
            manager.beginTransaction().replace(R.id.contactmee_frame_conner,fragment_QRC).commitAllowingStateLoss();
        }
    }
}
